package com.ellfors.module_first.dialog_queue

import android.content.Intent
import android.view.Gravity
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.ellfors.common.base.BaseComposeActivity
import com.ellfors.common.ext.logI
import com.ellfors.module_first.dialog_queue.DialogQueueManager.setOnDismissListenerWithQueue
import com.ellfors.module_first.dialog_queue.DialogQueueManager.showWithQueue

/**
 * @date: 2025/03/07
 * @author: ly
 * @Description: Dialog队列
 */
class DialogQueueActivity : BaseComposeActivity() {

    companion object {
        fun start(activity: AppCompatActivity) {
            Intent(activity, DialogQueueActivity::class.java).apply {
                activity.startActivity(this)
            }
        }

        private const val TYPE_DEFAULT = "Default"
        private const val TYPE_MIDDLE = "Middle"
        private const val TYPE_HIGH = "High"
        private const val TYPE_SYNC = "Sync"
    }

    @Composable
    override fun InitCompose() {
        Page()
    }

    override fun initData() {

    }

    private fun showDialog() {

//            "高优先级".buildDialog(TYPE_HIGH).showWithQueue(DialogQueueManager.PRIORITY_HIGH)

        "中优先级"
            .buildDialog(TYPE_MIDDLE)
            .showWithQueue(priority = DialogQueueManager.PRIORITY_MIDDLE)

        val fragment = TestDialogFragment.build("默认优先级")
        fragment.setDismissListener(object : TestDialogFragment.DismissListener {
            override fun onDismiss() {
                "TestDialogFragment Dismiss".logI()
            }
        })
        fragment.showWithQueue(this)

        "默认优先级".buildDialog(TYPE_DEFAULT).showWithQueue(
                priority = DialogQueueManager.PRIORITY_DEFAULT, intervalWithLast = 1000
            )

//            lifecycleScope.launch {
//
//                delay(4000)
//
//                "高优先级"
//                    .buildDialog(TYPE_MIDDLE)
//                    .showWithQueue(DialogQueueManager.PRIORITY_HIGH)
//            }
    }

    private fun String.buildDialog(type: String): TestDialog {
        val dialog = TestDialog(this@DialogQueueActivity).setTitle("这个是${type}").setContent(this)
        dialog.setOnDismissListenerWithQueue {
            "[Activity] [OnDismiss] $this".logI()
        }
        if (type == TYPE_SYNC) {
            dialog.window?.let { window ->
                window.setGravity(Gravity.TOP)
                val params = window.attributes
                params.y = 0
                window.attributes = params
            }
        }
        return dialog
    }

    @Composable
    fun Page() {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Button(onClick = { showDialog() }) {
                Text(text = "Show Dialog")
            }
        }
    }
}